/**
 *  登录表单控制器
 */
import tem from './index.html'
import $_fn from '~plugins'
class loginInputCtrl {
  constructor($scope,$rootScope,$timeout,$interval) {
    // let $scope = this
      this.rootfun = rootfun
      this.$timeout = $timeout
      let strParam = 'logIncodeImg'
      var token, timeSucc, timeErr
      //定义变量 用于添加class 高亮提醒用户（红色）
      $scope.error = {
          user:false,
          psw:false,
          code:false,
          phone:false,
          phoneCode:false,
          phoneCode1:false,
          resetpsw0:false,
          resetpsw1:false
      }
      //提示信息默认不显示 声明参数
      $scope.tishi = {
          succ:false,
          err:false
      }
      //  登录 声明参数 页面ng-model绑定传送数据到后台
      $scope.UnLogIn = {
          username:'',
          password:'',
          changeCode:'',
          token:''
      }
      //找回密码 页面ng-model绑定传送数据到后台
      $scope.findPsw = {
          phone:'',
          changeCode:'',
          token:''
      }
      //请输入手机验证码
      $scope.enterPsw = {
          phone:'',
          codes:''
      }
      //重置密码
      $scope.resetPsw = {
          psw0:'',
          psw1:'',
          phone:''
      }
      $scope.resetPsw.phone = $rootScope.userPhone
      console.log(token, timeSucc, timeErr)
      //ajax回调函数
      function getCode(result){
          //闭包函数 实现局部传参
          return cloCallback(result,strParam)
      }
      // 回调函数的闭包函数
      function cloCallback(result,str){
          $('.' + str).attr('src',result.content.img)
          //赋值token 重新发送给后端验证
          token = result.content.token
      }
      //点击表单是隐藏错误信息提醒
      $scope.hideErr = function(){
          $('.tixingFalse').css({'right':'-250px'})
          $scope.tishi.err = false
          $scope.tishi.succ = false
          for(var item in $scope.error){
              $scope.error[item] = false
          }
      }
      //错误信息显示提醒方法
      $scope.errorFun = function(str,num){
          $('.errorMes').html( str )
          //传入1 是为了区分使用了ajax后 必须用$apply
          if(num == 1){
              $scope.tishi.err = true
              animation0()
          }else{
              $scope.$apply(function(){
                  $scope.tishi.err = true
              })
              animation0()
          }
      }
      //错误提醒动画
      function animation0(){
          $('.tixingFalse').animate({
              'right':'48px',
          },500,function(){
              clearTimeout(timeErr)
              timeErr = setTimeout(function(){
                  $('.tixingFalse').css({'right':'-250px'})
                  $scope.tishi.err = false
                  $scope.$apply()
              },5000)
          })
      }
      //成功信息显示提醒方法
      $scope.successFun = function(str,num){
          $('.succ').html( str )
          //传入1 是为了区分使用了ajax后 必须用$apply
          if(num == 1){
              $scope.tishi.succ = true
              animation1()
          }else{
              $scope.$apply(function(){
                  $scope.tishi.succ = true
              })
              animation1()
          }
      }
      //成功提醒动画
      function animation1(){
          $('.tixingTrue').animate({
              'right':'48px',
          },500,function(){
              clearTimeout(timeSucc)
              timeSucc = setTimeout(function(){
                  $('.tixingTrue').css({'right':'-250px'})
                  $scope.tishi.succ = false
                  $scope.$apply()
              },5000)
          })
      }
      //btn点击获取验证码并写入img中   angular函数
      $scope.getLogInCode = function($event,params){
          if(params == 'logIncodeImg') {
              strParam = params//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
          }
          if(params == 'findPswImg'){
              strParam = params//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
          }
      }
      // 首次加载html发送ajax请求获取验证码
      $scope.defaultAjax = function(){
          $_fn._post('imageCode.do',getCode)
      }
      //登录、找回密码点击页面内跳转跳转   跳转发送ajax请求获取验证码
      $scope.logInLocationPath = function(params){

          // 回传登录成功模板切换
          $scope.$emit('template', params)
          if(params == 'findPsw'){
              strParam = 'findPswImg'//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
              console.log('ryy', 'findPsw')
          }
          if(params == 'logIn'){
              strParam = 'logIncodeImg'//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
          }
      }

      //btn点击获取验证码并写入img中   angular函数
      $scope.getLogInCode = function($event,params){
          if(params == 'logIncodeImg') {
              strParam = params//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
          }
          if(params == 'findPswImg'){
              strParam = params//更改需要更新图片的节点
              $_fn._post('imageCode.do',getCode)
          }
      }

      // 首次加载html发送ajax请求获取验证码
      $scope.defaultAjax = function(){
          $_fn._post('imageCode.do',getCode)
      }
      //键盘回车时提交表单
      $scope.key = function(e,param){
          var keycode = window.event?e.keyCode:e.which
          if(keycode == 13){                //回车触发
              if(param == 'logIn'){           //登录界面点击提交触发的方法
                  $scope.logInSuccess('logInSuccess')
              }
              if(param == 'find'){            //找回密码
                  $scope.findPswFun('enterPsw')
              }
              if(param == 'enter'){           //找回密码-输入验证码
                  $scope.enterPswFun()
              }
              if(param == 'reset'){           //找回密码-重置密码
                  $scope.resetPswFun()
              }
          }
      }

      //重置密码
  $scope.resetPswFun = function(){
    console.log(11)
    if(/^\s*$/.test($scope.resetPsw.psw0)){
      $scope.errorFun('新密码不能为空',1)
      $scope.error.resetpsw0 = true
      return
    }
    if(/^\s*$/.test($scope.resetPsw.psw1)){
      $scope.errorFun('再次新密码不能为空',1)
      $scope.error.resetpsw1 = true
      return
    }
    if( $scope.resetPsw.psw0 != $scope.resetPsw.psw1 ){
      $scope.errorFun('密码不一致',1)
      $scope.error.resetpsw0 = true
      $scope.error.resetpsw1 = true
      return
    }
    http.post('resetPassword.do',{phone:$scope.resetPsw.phone,password:$scope.resetPsw.psw0},function(result){
      if(result.type == 1){
        $scope.successFun(result.msg)
        $scope.$emit('template', 'logIn');
        var itimer = $interval(function(){
          var aa = $('.intervalSecond').html()
          aa--
          $('.intervalSecond').html(aa)
          if(aa == 0){
            $interval.cancel(itimer)
            $scope.$emit('template', 'logIn');
          }
        },1000)
      }else{
        $scope.errorFun(result.msg)
      }
    })

  }

  }
}
loginInputCtrl.$inject = ['$scope','$rootScope','$timeout','$interval']
export default function() {
    return {
        restrict: 'E',
        template: tem,
        controller: loginInputCtrl
    }
}